<template>
  <div class="login" :style="eabg">
    <!-- <img src="../../assets/login/loginbg.png" alt="" class="loginbg"> -->
    <div class="loginConent">
      <div class="loginTop">
        <img src="../../assets/login/logo.png" alt class="logo" />
        <div class="logotext">注册</div>
      </div>
      <div class="content">
        <div class="item">
          <span class="iconfont icon-lianxiren1"></span>
          <input type="number" placeholder="请输入手机号" v-model="paramData.tel" maxlength="11" />
        </div>
        <div class="item">
          <span class="iconfont icon-yanzhengyanzhengma"></span>
          <input
            type="number"
            placeholder="请输入验证码"
            class="codeinput"
            v-model="paramData.code"
            maxlength="6"
          />
          <div
            :class="[isSmall == true?'getcodesmall':'getcode']"
            @click="getcode(paramData.tel)"
          >{{ctext}}</div>
        </div>
        <!-- <div class="item">
          <span class="iconfont icon-yaoqing"></span>
          <input type="text" placeholder="请输入邀请码" v-model="paramData.invitationcode" maxlength="11" />
        </div> -->
        <div class="item item1">
          <span class="iconfont icon-login_mima"></span>
          <input type="password" placeholder="请设置密码(不超过8位)" v-model="paramData.password" maxlength="8" />
        </div>
        <div class="item item2">
          <span class="iconfont icon-login_mima"></span>
          <input type="password" placeholder="请确认密码" v-model="paramData.aginpassword" maxlength="8" />
        </div>
      </div>
      <div
        class="loginBtn"
        @click="register(paramData.tel,paramData.code,paramData.invitationcode,paramData.password,paramData.aginpassword)"
      >
        <img src="../../assets/login/login.png" alt class="img" />
        <div class="logintext">注&nbsp;&nbsp;册</div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      paramData: {
        tel: "",
        code: "",
        invitationcode: "",
        password: "",
        aginpassword: ""
      },
      count: 60,
      ctext: "获取验证码",
      isSmall: false,
      pid: "",
      id: "",
      eabg: {
        backgroundImage:
          "url(" + require("../../assets/login/loginbg.png") + ")",
        backgroundRepeat: "no-repeat",
        backgroundSize: "100% 100%"
      }
    };
  },
  created() {
    if (this.$route.query.pid || this.$route.query.id) {
      this.pid = this.$route.query.pid;
      this.id = this.$route.query.id;
    }
  },
  methods: {
    getcode(tel) {
      var that = this;
      var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/; //手机号验证
      var settime = function(that) {
        var timeOut;
        if (that.count == 0) {
          that.ctext = "获取验证码";
          that.count = 60;
          clearTimeout(timeOut);
          that.isSmall = false;
          return;
        } else {
          that.ctext = "重新发送" + that.count + "秒";
          that.count--;
          that.isSmall = true;
        }
        setTimeout(function() {
          settime(that);
        }, 1000);
      };
      if (tel == "") {
        this.AlertWin("手机号不能为空");
      } else if (!myreg.test(tel)) {
        this.AlertWin("请输入正确的手机号！");
      } else if (that.ctext == "获取验证码") {
        settime(that);
        // var url = that.$host + 'www/met/front/get_code'
        var data = {
          mobile: tel
        };
        var url = this.$host1 + `home/sendCode`;
        this.$axios.post(url, data).then(res => {
            var resData = res.data;
            console.log(resData);
            if (resData.code == 200) {
              this.AlertWin("发送成功！");
              setTimeout(()=>{
                  //that.paramData.code = resData.vcode;
              },3000)
              
            } else {
              this.AlertWin(resData.message);
            }
          })
          .catch(() => {
            this.AlertWin("网络错误！请稍后重试！");
          });
      }
      
    },
    register(tel, code, invitationcode, password, aginpassword) {
      //console.log(tel, code, invitationcode, password, aginpassword);
      //return;
      if (tel == "" || tel == undefined || tel == null) {
        this.AlertWin("手机号码不能为空");
        return false;
      } else if (code == "" || code == undefined || code == null) {
        this.AlertWin("验证码不能为空");
        return false;
      }
      else if (password == "" || password == undefined || password == null) {
        this.AlertWin("密码不能为空");
        return false;
      } else if (
        aginpassword == "" ||
        aginpassword == undefined ||
        aginpassword == null
      ) {
        this.AlertWin("确认密码不能为空");
        return false;
      } else {
        if (password !== aginpassword) {
          this.AlertWin("两次密码不一致");
        } else if (!/^1[3456789]\d{9}$/.test(tel)) {
          this.AlertWin("请填写有效的手机号码");
        } else {
          // var url = this.$host + `www/met/front/register`
          var data = {
            mobile: tel,
            vcode: code,
            // "invitation_code":invitationcode,
            password: password,
            re_password: aginpassword,
            //pid: this.pid
          };
          var url = this.$host1 + `home/signIn`;
          this.$axios.post(url,data).then(res => {
              console.log(res)
              //return
              if (res.status == 200) {
                var resData = res.data;
                if (resData.code == 10030) {
                //   if (this.$route.query.id) {
                //     localStorage.setItem("id", this.id);
                //   }
                  //this.getWXuserInfo(resData.user_id);

                  this.AlertWin("恭喜您注册成功");
                  setTimeout(()=>{
                      this.$router.push('/userinfo')
                  },2000)
                } else {
                  this.AlertWin(resData.message);
                }
              }
            })
            .catch(() => {
              this.AlertWin("网络出错,请检查网络设置");
            });
        }
      }
    },
    getWXuserInfo(uerid) {
      let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4d891cfd89a934e7&redirect_uri=https%3a%2f%2ftest.izhixiu.cn%2fwww%2fmet%2fmusicstore%2f&response_type=code&scope=snsapi_userinfo&state=${uerid}&#wechat_redirect`;
      window.location.href = url;
    }
  }
};
</script>
<style lang="scss">
.login {
  width: 100%;
  height: 100vh;
  .loginbg {
    width: 100%;
    height: 100%；;
  }
  .loginConent {
    width: 100%;
    height: auto;
    position: absolute;
    left: 0;
    top: 0;
    padding: 0.58rem 0.4rem;
    box-sizing: border-box;
    .loginTop {
      width: 7.02rem;
      margin: 0 auto;
      display: flex;
      align-items: center;
      .logo {
        width: 0.78rem;
        height: 0.78rem;
        // background-color: #297ef2;
        border-radius: 0.14rem;
      }
      .logotext {
        font-size: 0.4rem;
        color: #666666;
        margin-left: 0.28rem;
      }
    }
    .content {
      margin: 0 auto;
      margin-top: 1.55rem;
      .item {
        width: 5.4rem;
        height: 1rem;
        display: flex;
        align-items: center;
        border-bottom: 0.01rem solid #e9e9e9;
        .itemimg {
          width: 0.44rem;
          height: 0.46rem;
        }
        input {
          width: 5.4rem;
          // padding-top:5px;
          border: 0;
          outline: 0;
          background: none;
          //background-color: greenyellow;
          height: 1rem;
          margin-left: 0.1rem;
          //margin-top:0.3rem;
          font-size: 0.32rem;
          color: #999999;
        }
        .codeinput {
          width: 3rem;
        }
        .getcode {
          // width:168px;
          font-size: 0.32rem;
          color: #f44349;
        }
        .getcodesmall {
          // width:168px;
          font-size: 0.26rem;
          color: #f44349;
        }
        .iconfont {
          font-size: 0.48rem;
          color: #5a01a3;
          margin-top: 0.1rem;
        }
      }
      .item1 {
        width: 4.7rem;
      }
      .item2 {
        width: 3.7rem;
      }
    }
    .loginBtn {
      width: 5.4rem;
      height: 1rem;
      margin-top: 1.1rem;
      display: flex;
      align-items: center;
      position: relative;
      .img {
        width: 5.4rem;
        height: 1rem;
      }
      .logintext {
        font-size: 0.32rem;
        color: #f5f5f5;
        position: absolute;
        // top:30px;
        left: 1.5rem;
      }
    }
  }
}
</style>
